from datetime import datetime

from ext import db


class User(db.Model):
    # 数据表名称
    # __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    real_name = db.Column(db.String(60), unique=True, nullable=True, default='')
    gender = db.Column(db.Integer, nullable=True, default=0)
    # 注册时间: 需要传入字符串格式的日期
    create_time = db.Column(db.DateTime, default=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

    # 将实例对象转换为字典类型
    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'real_name': self.real_name,
            'gender': self.gender,
            'create_time': self.create_time,
        }